package com.acme.bankapp.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.naming.spi.DirStateFactory.Result;

import com.acme.bankapp.domain.Bank;
import com.acme.bankapp.domain.Client;
import com.acme.bankapp.domain.accounts.Account;
import com.acme.bankapp.domain.accounts.AccountType;

public class BankReport {
	
	public int getNbOfCustomers(Bank bank) {
		return bank.getClients().size();
	}

	public int getNbOfAccounts(Bank bank) {
		int num = 0;
		for (Client client : bank.getClients()) {
			num += client.getAccounts().size();
		}
		return num;
	}

	public Collection<Account> getAccountsSortedBySum(Bank bank) {
		ArrayList<Account> result = new ArrayList<Account>();
		for (Client client : bank.getClients()) {
			for (Account account : client.getAccounts()) {
				result.add(account);
			}
		}
		
		Collections.sort(result);
		return result;
	}

	public double getBankCredit(Bank bank) {
		double result = 0.;
		for (Client client : bank.getClients()) {
			for (Account account : client.getAccounts()) {
				if ((account.getAccountType() == AccountType.CHECKING) 
					&& (account.getBalance() < 0.) ){
					result += account.getBalance();
				}
			}
		}
		return -result;
	}

	public Map<Client, HashSet<Account>> getCustomerAccounts(Bank bank) {
		Map<Client, HashSet<Account>> result = new HashMap<Client, HashSet<Account>>();
		for (Client client : bank.getClients()) {
			result.put(client, new HashSet<>(client.getAccounts()));
		}
		return result;
	}

}
